
#include <stdio.h>
#include <math.h>

#include "MouseAndCat.Win32Lib.h"

static char debug[512] = {0};
static const int XFFFF = (int)pow((float)sizeof(char), (int)sizeof(int));

MouseEventCallback Win32LibHelper::mouse_event_callback_ = NULL;

void Win32LibHelper::SetMouseEventCallback(MouseEventCallback callback)
{
	mouse_event_callback_ = callback;
}

void Win32LibHelper::CallMouseEventCallback(MSLLHOOKSTRUCT* hook_struct)
{
	if(mouse_event_callback_ != NULL)
		mouse_event_callback_(hook_struct);
}

void ParseEvent(WPARAM wParam, LPARAM lParam)
{
	MSLLHOOKSTRUCT* mouse_info = (MSLLHOOKSTRUCT*)lParam;

	Win32LibHelper::CallMouseEventCallback(mouse_info);

	if(wParam == WM_MOUSEMOVE)
		sprintf(debug, "Mouse moved: %ld, %ld\n", mouse_info->pt.x, mouse_info->pt.y);
	else if(wParam == WM_MOUSEWHEEL)
		sprintf(debug, "Mouse wheel: %u (%lu)\n", ((mouse_info->mouseData >> sizeof(long)) / 2) & XFFFF, mouse_info->mouseData);
	else
		sprintf(debug, "Mouse event\n");

	OutputDebugStringA(debug);
}

LRESULT CALLBACK MouseHookCallback(int code, WPARAM wParam, LPARAM lParam)
{
	if(code >= 0)
		ParseEvent(wParam, lParam);
	
	return CallNextHookEx(NULL, code, wParam, lParam);
}
